﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class EditBrand : BasePage
    {
        private IBrandBLL brandBLL = IocContainer.Instance.Resolve<IBrandBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int brandId = 0;
                    int.TryParse(Request.QueryString["BrandId"], out brandId);
                    if (brandId > 0)
                    {
                        Skyworth.CRM.Service.Entity.Brand brand = brandBLL.GetBrandById(brandId);
                        this.txtBrandName.Text = brand.BrandName;
                        this.txtBrandCode.Text = brand.BrandCode;
                        this.txtDescription.Text = brand.Description;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                Skyworth.CRM.Service.Entity.Brand brand = new Skyworth.CRM.Service.Entity.Brand();
                brand.BrandName = txtBrandName.Text.Trim();
                brand.BrandCode = txtBrandCode.Text.Trim();
                brand.Description =txtDescription.Text.Trim();

                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    brandBLL.Add(brand);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int brandId = 0;
                    int.TryParse(Request.QueryString["BrandId"], out brandId);
                    brand.BrandId = brandId;
                    brandBLL.update(brand);
                    isOK = true;
                }

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
